using System;
using System.IO;
namespace ComponentAce.Compression.Libs.zlib
{
	public class ZInputStream : BinaryReader
	{
		protected ZStream zstream_0 = new ZStream();
		protected int bufsize = 512;
		protected int flush;
		protected byte[] byte_0;
		protected byte[] buf1 = new byte[1];
		protected bool compress;
		internal Stream in_Renamed;
		internal bool nomoreinput;
		public virtual int FlushMode
		{
			get
			{
				return this.flush;
			}
			set
			{
				this.flush = value;
			}
		}
		public virtual long TotalIn
		{
			get
			{
				return this.zstream_0.total_in;
			}
		}
		public virtual long TotalOut
		{
			get
			{
				return this.zstream_0.total_out;
			}
		}
		internal void InitBlock()
		{
			this.flush = 4;
			this.byte_0 = new byte[this.bufsize];
		}
		public ZInputStream(Stream in_Renamed) : base(in_Renamed)
		{
			this.InitBlock();
			this.in_Renamed = in_Renamed;
			this.zstream_0.inflateInit();
			this.compress = false;
			this.zstream_0.next_in = this.byte_0;
			this.zstream_0.next_in_index = 0;
			this.zstream_0.avail_in = 0;
		}
		public ZInputStream(Stream in_Renamed, int level) : base(in_Renamed)
		{
			this.InitBlock();
			this.in_Renamed = in_Renamed;
			this.zstream_0.deflateInit(level);
			this.compress = true;
			this.zstream_0.next_in = this.byte_0;
			this.zstream_0.next_in_index = 0;
			this.zstream_0.avail_in = 0;
		}
		public override int Read()
		{
			if (this.read(this.buf1, 0, 1) == -1)
			{
				return -1;
			}
			return (int)(this.buf1[0] & 255);
		}
		public int read(byte[] byte_1, int int_0, int int_1)
		{
			if (int_1 == 0)
			{
				return 0;
			}
			this.zstream_0.next_out = byte_1;
			this.zstream_0.next_out_index = int_0;
			this.zstream_0.avail_out = int_1;
			while (true)
			{
				if (this.zstream_0.avail_in == 0 && !this.nomoreinput)
				{
					this.zstream_0.next_in_index = 0;
					this.zstream_0.avail_in = SupportClass.ReadInput(this.in_Renamed, this.byte_0, 0, this.bufsize);
					if (this.zstream_0.avail_in == -1)
					{
						this.zstream_0.avail_in = 0;
						this.nomoreinput = true;
					}
				}
				int num;
				if (this.compress)
				{
					num = this.zstream_0.deflate(this.flush);
				}
				else
				{
					num = this.zstream_0.inflate(this.flush);
				}
				if (this.nomoreinput && num == -5)
				{
					break;
				}
				if (num != 0 && num != 1)
				{
					goto Block_6;
				}
				if (this.nomoreinput && this.zstream_0.avail_out == int_1)
				{
					return -1;
				}
				if (this.zstream_0.avail_out != int_1)
				{
					goto IL_156;
				}
				if (num != 0)
				{
					goto Block_10;
				}
			}
			return -1;
			Block_6:
			throw new ZStreamException((this.compress ? "de" : "in") + "flating: " + this.zstream_0.string_0);
			Block_10:
			IL_156:
			return int_1 - this.zstream_0.avail_out;
		}
		public long skip(long long_0)
		{
			int num = 512;
			if (long_0 < 512L)
			{
				num = (int)long_0;
			}
			byte[] array = new byte[num];
			return (long)SupportClass.ReadInput(this.get_BaseStream(), array, 0, array.Length);
		}
		public override void Close()
		{
			this.in_Renamed.Close();
		}
	}
}
